package com.xc.ksxt.controller;

import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.Helper;
import com.xc.db.tables.Tag;
import com.xc.ksxt.entity.TagEntity;
import com.xc.ksxt.service.TagService;
import com.xc.ksxt.vo.TagVO;
import com.xc.sys.entity.SysDictEntity;
import com.xc.sys.service.SysDictService;
import com.xc.sys.service.SysUserService;
import com.xc.sys.vo.SysUserVO;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tag")
@CrossOrigin
public class TagController {

    @Autowired
    private TagService tagService;
    private Tag TAG = Tag.TAG.as("TAG");
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getPageList"})
    public LayuiTableResponse<TagVO> getPageList(HttpServletRequest request) {
        LayuiTableResponse<TagVO> ltr = new LayuiTableResponse<>();
        Condition cond = TAG.DEL_FLAG.notEqual(-1);
        String name = request.getParameter("name");
        String groupId = request.getParameter("groupId");
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer page = Integer.valueOf(request.getParameter("page"));
        if (StringUtils.isNotBlank(name)) {
            cond = cond.and(TAG.NAME.like("%"+name+"%"));
        }
        if (StringUtils.isNotBlank(groupId)) {
            cond = cond.and(TAG.GROUP_ID.eq(Integer.valueOf(groupId)));
        }
        Long count = tagService.count(cond);
        ltr.setCount(count);
        PageInfo<TagEntity> pageInfo = new PageInfo<>(limit, page);
        Result<Record> list = tagService.select(cond, pageInfo,TAG.ORDERNO.asc(),TAG.NAME.asc(),TAG.CREATE_TIME.desc());
        List<TagVO> tagEntityList = list.into(TagVO.class);
        for (TagVO tagEntity : tagEntityList){
            SysUserVO user = SysUserService.getFrameCaches(tagEntity.getCreateUserId());
            tagEntity.setCreateUserName(user==null?"":user.getName());
            SysDictEntity dict = SysDictService.getFromCaches("rvalid-state," + tagEntity.getState());
            tagEntity.setStateTxt(dict==null?"":dict.getName());
        }
        ltr.setData(tagEntityList);
        return ltr;
    }

    @RequestMapping({"/getById"})
    public TagEntity getById(Integer id) {
        TagEntity entity = tagService.getEntityById(id);
        return entity;
    }

    @RequestMapping({"/save"})
    public ResponseEntity<TagEntity> save(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
        ResponseEntity<TagEntity> res = new ResponseEntity<>();
        TagEntity tagEntity = Helper.map2Bean(new TagEntity(), params);
        tagService.save(tagEntity);
        return res;
    }
    @RequestMapping({"/delete"})
    public LayuiTableResponse<TagEntity> delete(@RequestBody Map<String,String> params) {
        String ids = params.get("ids");
        Integer[] idarr = Helper.str2integerArr(ids);
        LayuiTableResponse<TagEntity> ltr = new LayuiTableResponse<>();
        for (Integer id : idarr) {
            TagEntity m = tagService.getEntityById(id);
            m.setDelFlag(-1);
            tagService.update(m);
        }
        return ltr;
    }

}
